package me.ingxin.android.tools.app.drawer

import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentStatePagerAdapter
import androidx.recyclerview.widget.LinearLayoutManager
import me.ingxin.android.badge.DotBadge
import me.ingxin.android.badge.NumberBadge
import me.ingxin.android.tools.app.databinding.FragmentPageBinding
import me.ingxin.android.tools.app.databinding.FragmentTab1Binding

/**
 * Created by ingxin on 2022/5/27
 */
class Tab1Fragment : Fragment() {

    private lateinit var binding: FragmentTab1Binding

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = FragmentTab1Binding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)


        binding.viewPager.adapter = object : FragmentStatePagerAdapter(childFragmentManager) {
            override fun getCount(): Int {
                return 3
            }

            override fun getItem(position: Int): Fragment {
                return Page1Fragment()
            }

            override fun getPageTitle(position: Int): CharSequence? {
                return "tab$position"
            }

        }

        binding.tabLayout.apply {
            setTabTextColors(Color.parseColor("#60000000"),Color.parseColor("#000000"))
            setupWithViewPager(binding.viewPager)
        }

        binding.tabLayout.getTabAt(0)?.view?.let {
            val badge = DotBadge.Builder().build(it)
            badge.setOffset(-20,20)
        }

        binding.tabLayout.getTabAt(1)?.view?.let {
            val badge = NumberBadge.Builder().build(it)
            badge.setNumb(99)
            badge.setOffset(-20,20)
        }


    }
}


class Page1Fragment : Fragment() {

    private lateinit var binding: FragmentPageBinding

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = FragmentPageBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val adapter = PageTxtAdapter()
        for (i in 0 until 20) {
            adapter.datas.add("测试数据 $i")
        }
        binding.rv.layoutManager = LinearLayoutManager(requireContext())
        binding.rv.adapter = adapter
    }


}